package com.wanmait.spacesys.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.wanmait.spacesys.util.DbHelper;
import com.wanmait.spacesys.vo.Userinfo;

public class UserinfoDAO {
	
	
	//用户注册(添加)
	public void insert(Userinfo userinfo) {
		String sql = "INSERT INTO userinfo(nickname,username,PASSWORD) VALUE(?,?,?);";
		DbHelper.update(sql, userinfo.getNickname(),
							userinfo.getUsername(),
							userinfo.getPassword()
							);
	}
	
	//判断用户是否重复
	public int isExist(String username) {
		//总共有几条数据
		int result = 0;
		String sql = "select count(*) as count from userinfo where username=?";
		Connection conn = DbHelper.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,username);
			rs = ps.executeQuery();
			if(rs.next()) {
				result = rs.getInt("count");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DbHelper.close(rs, ps, conn);
		}
		return result;
		}
	
	//判断用户是否正确,通过账号密码查出所有的用户
	public Userinfo isRight(String username,String password) {
		Userinfo userinfo = null;
		String sql = "SELECT * FROM userinfo WHERE username=? AND password=?";
		Connection conn = DbHelper.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if(rs.next()) {
				userinfo = new Userinfo();
				userinfo.setId(rs.getInt("id"));
				userinfo.setUsername(username);
				userinfo.setPassword(password);
				userinfo.setNickname(rs.getString("nickname"));
				userinfo.setRegisteTime(rs.getTimestamp("registe_time"));
				userinfo.setHeaderPic(rs.getString("header_pic"));
				userinfo.setEnable(rs.getBoolean("enable"));
				userinfo.setPhone(rs.getString("phone"));
				userinfo.setReport(rs.getInt("report"));
				userinfo.setPraise(rs.getInt("praise"));
				userinfo.setPosting(rs.getInt("posting"));
				userinfo.setCollection(rs.getInt("collection"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DbHelper.close(rs, ps, conn);
		}
		return userinfo;
	}
	
	
	    //通过用户id获得用户的信息(登录用户)
		public Userinfo userinfoFindById(Integer userinfoId) {
			Userinfo userinfo = null;
			String sql = "SELECT * FROM userinfo WHERE id=?";
			Connection conn = DbHelper.getConnection();
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				ps = conn.prepareStatement(sql);
				ps.setInt(1, userinfoId);
				rs = ps.executeQuery();
				if(rs.next()) {
					userinfo = new Userinfo();
					userinfo.setId(userinfoId);
					userinfo.setUsername(rs.getString("username"));
					userinfo.setPassword(rs.getString("password"));
					userinfo.setNickname(rs.getString("nickname"));
					userinfo.setRegisteTime(rs.getTimestamp("registe_time"));
					userinfo.setHeaderPic(rs.getString("header_pic"));
					userinfo.setEnable(rs.getBoolean("enable"));
					userinfo.setPhone(rs.getString("phone"));
					userinfo.setReport(rs.getInt("report"));
					userinfo.setPraise(rs.getInt("praise"));
					userinfo.setPosting(rs.getInt("posting"));
					userinfo.setCollection(rs.getInt("collection"));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DbHelper.close(rs, ps, conn);
			}
			return userinfo;
		}
		
		
		//测试通过用户id获得用户的信息(登录用户)
		public static void main(String[] args) {
			UserinfoDAO userinfoDAO=new UserinfoDAO();
			Userinfo userinfo=userinfoDAO.userinfoFindById(2);
			System.out.println(userinfo.getUsername()+userinfo.getNickname()+userinfo.getPhone()+userinfo.getPassword()+userinfo.getHeaderPic());
		}
}
